package pikasial.graphique;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.Comparator;

import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JToolBar;
import javax.swing.event.CaretEvent;
import javax.swing.event.CaretListener;

import pikasial.images.PhotosListModel;

public class PanneauRechercheTri extends JToolBar {


	private static final long serialVersionUID = 255292499640018045L;

	private JTextField rechercheNom;
	private JComboBox tri;

	private PhotosListModel plm;

	private int previousCaretPosition;
	private final String chemin = "Chemin";
	private final String dateC = "Date de création";
	private final String dateM = "Date de modification";
	private final String invite = "Entrez un nom...";
	public PanneauRechercheTri() {
		super();
		initComponents();
	}

	public PanneauRechercheTri(PhotosListModel plm) {
		this();
		setAlbumModel(plm);
	}

	private void initComponents() {
		new BoxLayout(this,BoxLayout.X_AXIS);

		setFloatable(false);
		rechercheNom = new JTextField(invite);
		rechercheNom.setEditable(true);
		rechercheNom.setColumns(15);
		rechercheNom.addMouseListener(new MouseListener() {

			public void mouseClicked(MouseEvent arg0) {
				if (arg0.getButton() == MouseEvent.BUTTON1 &&
						rechercheNom.getText().equals(invite)) {
					rechercheNom.setText("");
					previousCaretPosition = 0;
				}
			}
			public void mouseEntered(MouseEvent arg0) {}
			public void mouseExited(MouseEvent arg0) {}
			public void mousePressed(MouseEvent arg0) {}

			public void mouseReleased(MouseEvent arg0) {

			}
		});

		rechercheNom.addCaretListener(new CaretListener() {
			public void caretUpdate(CaretEvent arg0) {
				if (!rechercheNom.getText().equals(invite) && plm != null) {
					int position = arg0.getDot();
					if (position <= previousCaretPosition) {
						plm.synchroniserAvecAlbum();
						if (position != 0)
							plm.rechercherNom(rechercheNom.getText());
					} else if (position > previousCaretPosition) {
						plm.rechercherNom(rechercheNom.getText());
					}
					previousCaretPosition = position;
				}
			}
		});

		tri = new JComboBox();
		tri.setEditable(false);
		tri.addItem(chemin);
	//	tri.addItem(dateC);
		tri.addItem(dateM);
		tri.setSelectedItem(chemin);
		tri.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent ae) {
				if (plm != null) {
					new Thread(new Runnable() {
						public void run() {
							plm.trierPar(getSelectedComparator());
						}
					}).start();
				}
			}
		});
		JLabel rech = new JLabel("Rechercher ");
		rech.setIcon( new ImageIcon(getClass().getResource(pikasial.utils.PikasialConstantes.SEARCH_ICON_PATH)));
		rech.setLabelFor(rechercheNom);
		add(rech);

		add(rechercheNom);
		addSeparator();
		add(new JLabel("Trier par :"));
		add(tri);
	}


	public void setAlbumModel(PhotosListModel plm) {
		this.plm = plm;
		rechercheNom.setEnabled(plm != null);
		tri.setEnabled(plm != null);
		previousCaretPosition = 0;
	}

	public Comparator<pikasial.images.PikImage> getSelectedComparator() {
		String s = (String) tri.getSelectedItem();
		Comparator<pikasial.images.PikImage> comp = null;
		if (s != null) {
			if (s.equals(chemin))
				comp = PhotosListModel.PAR_CHEMIN;
			else if (s.equals(dateC))
				comp = null;//PhotosListModel.PAR_DATE_CREATION;
			else if (s.equals(dateM))
				comp = PhotosListModel.PAR_DATE_MODIF;
		}
		return comp;
	}

}
